<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<h2>员工信息</h2>
<table class="table  table-hover" style="text-align:center;">

	<thead>
		<tr class="active">
		   
			<th style="text-align:center;">员工id</th>
			<th style="text-align:center;">员工姓名</th>
			<th style="text-align:center;">员工工资</th>
			<th style="text-align:center;"  >操作</th>
			 <td>
			      <input   type="checkbox"  class="checkAll"  />全选
			      <!--  <input   type="checkbox"    /> -->
			      <a  class="checkEmps">多选</a>
			 </td>
	  </tr>
	</thead>
	<tbody>
		<c:forEach items="${emps}" var="e">
			<tr class="warning">
				<td>${e.emp_id}</td>
				<td>${e.emp_name}</td>
				<td>${e.emp_sal}</td>
				<td>
				       <button type="button" class="btn btn-success   bn_delete"   data_id="${e.emp_id}">删除</button>
				       <button type="button" class="btn btn-success   bn_update"   data_id="${e.emp_id}" >修改</button>
				</td>
				 <td><input   type="checkbox"    name="eid"   value="${e.emp_id}"/></td>
			</tr>
		</c:forEach>
	</tbody>
</table>

<div>
	<button type="button" class="btn btn-success  " id="addEmp">
		添加员工<span class="glyphicon glyphicon-plus"></span>
	</button>
	<button type="button" class="btn btn-success  " id="deleteEmp">
		删除员工<span class="glyphicon glyphicon-trash"></span>
	</button>
</div>

<script>
//全选
$(".checkAll").click(function() {
	$(':checkbox[name=eid]').prop("checked", this.checked);
});


$(".checkEmps").click(function() {
	var ids = [];
		$(':checkbox[name=eid]:checked').each(function() {
		ids.push($(this).val());
	})
	alert(ids);
	$.ajax({
					url : 'emp.do?operate=deleteEmps',
					data : {
						ids : ids
					},
					method : 'post',
					dataType : 'json'
				}).done(function() {
					alert("成功");
				})
});



	var dlg;
	$('#addEmp').click(function() {
		$.ajax({
			url : 'addEmp.jsp',
			method : 'post',
			cache : false,
			dataType : 'html'
		}).done(function(html) {
			dlg = new MyDialog({
				content : html
			});
		});
	});

	
	$('.bn_delete').click(function() {
		var data_id = $(this).attr("data_id");
		swal({
			  title: "Are you sure?",
			  text: "Once deleted, you will not be able to recover this imaginary file!",
			  icon: "warning",
			  buttons: ["取消", "确认删除"],
			  dangerMode: true,
			})
			.then((willDelete) =>{
			  if (willDelete) {
					$.ajax({
						url : "emp.do",
						method : 'post',
						cache : false,
						data : {"data_id" : data_id,
							"operate":"deleteEmp"},
						dataType : "json"
					}).done(function(m) {
						if (m.m == '1') {//成功
			                
							aa();
						} else {//失败
							swal({
								  title: "删除失败",
								  icon: "warning",
								  buttons: true,
								  dangerMode: true,
								})
						}
					});
			    swal("Poof! Your imaginary file has been deleted!", {
			      icon: "success",
			    });
			  } else {
			    swal("Your imaginary file is safe!");
			  }
			});

	}); 

	
 	 $(".bn_update").click(function(){
 		var id = $(this).attr("data_id");
	    	$.ajax({
	    		url:"emp.do",
	    		method:"post",
	    		cache : false,
	    		data:{
	    			"operate":"queryById",
	    			"id":id
					},
					dataType:"html"
	    	}).done(function(html){
	    		dlg = new MyDialog({
					content : html
				});
	    	});
	    });
</script>